﻿//************************************************************************
//* Written by Jirat Boomuang
//* Filename: ApplicationMain.cs
//* Revision: 1.0
//* Author: JiratB
//* Platform: .NET Framework 4.0
//* Description: This file contains the code for creating entities and 
//*              fields from CSV files.
//************************************************************************

//************************************************************************
//* Revision History
//------------------------------------------------------------------------
//* Revision  Date      Signature    Description
//------------------------------------------------------------------------
//* 1.0       20131115  JiratB       - First release
//*                                    Note: OptionSet is not included
//*                                          Views and Forms are not included
//************************************************************************

//-- For Standard Components ---------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

//-- Added by JiratB, 20130601 --
using System.ServiceModel;
using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

using Microsoft.Crm.Sdk.Messages;

using AdaCRMUtilities;
using AdaCSVUtilities;
//--------------------------------

namespace CrmSdkConsoleApplication
{
    class AdaEntityMap
    {
        #region Constants
        public const string HEADER_SCHEMANAME = "SchemaName";
        public const string HEADER_DISPLAYNAME_EN = "DisplayName_EN";
        public const string HEADER_DISPLAYCOLLECTIONAME_EN = "DisplayCollectionName_EN";
        public const string HEADER_DESCRIPTION_EN = "Description_EN";
        public const string HEADER_DISPLAYNAME_TH = "DisplayName_TH";
        public const string HEADER_DISPLAYCOLLECTIONNAME_TH = "DisplayCollectionName_TH";
        public const string HEADER_DESCRIPTION_TH = "Description_TH";
        public const string HEADER_OWNERSHIPTYPE = "OwnershipType";
        public const string HEADER_ISACTIVITY = "IsActivity";
        public const string HEADER_P_SCHEMANAME = "P_SchemaName";
        public const string HEADER_P_REQUIREDLEVEL = "P_RequiredLevel";
        public const string HEADER_P_MAXLENGTH = "P_MaxLength";
        public const string HEADER_P_FORMAT = "P_Format";
        public const string HEADER_P_DISPLAYNAME_EN = "P_DisplayName_EN";
        public const string HEADER_P_DESCRIPTION_EN = "P_Description_EN";
        public const string HEADER_P_DISPLAYNAME_TH = "P_DisplayName_TH";
        public const string HEADER_P_DESCRIPTION_TH = "P_Description_TH";

        public const string HEADER_F_ATTRIBUTEFILE = "AttributeFile";
        public const string HEADER_F_RELATIONSHIPFILEOTM = "RelationshipFileOTM";
        public const string HEADER_F_RELATIONSHIPFILEMTM = "RelationshipFileMTM";
        #endregion

        #region MapEntityField()
        //-- method MapEntityField() --
        //-- Map input to entity field by data type --
        public static void MapEntityField(string columnName, string input, EntityMetadata entity, StringAttributeMetadata primaryField)
        {
            if (columnName == HEADER_SCHEMANAME)
            {
                entity.SchemaName = input;
            }
            else if (columnName == HEADER_DISPLAYNAME_EN)
            {
                Label displayNameLabel;
                if (entity.DisplayName == null)
                    displayNameLabel = new Label();
                else
                    displayNameLabel = entity.DisplayName;

                LocalizedLabel englishLabel = new LocalizedLabel(
                        input,
                        1033
                    );

                displayNameLabel.LocalizedLabels.Add(englishLabel);

                entity.DisplayName = displayNameLabel; 
            }
            else if (columnName == HEADER_DISPLAYCOLLECTIONAME_EN)
            {
                Label displayNameLabel;
                if (entity.DisplayCollectionName == null)
                    displayNameLabel = new Label();
                else
                    displayNameLabel = entity.DisplayCollectionName;

                LocalizedLabel englishLabel = new LocalizedLabel(
                        input,
                        1033
                    );

                displayNameLabel.LocalizedLabels.Add(englishLabel);

                entity.DisplayCollectionName = displayNameLabel;
            }
            else if (columnName == HEADER_DESCRIPTION_EN)
            {
                entity.Description = new Label(input, 1033);
            }
            else if (columnName == HEADER_DISPLAYNAME_TH)
            {
                Label displayNameLabel;
                if (entity.DisplayName == null)
                    displayNameLabel = new Label();
                else
                    displayNameLabel = entity.DisplayName;

                LocalizedLabel thaiLabel = new LocalizedLabel(
                        input,
                        1054
                    );

                displayNameLabel.LocalizedLabels.Add(thaiLabel);

                entity.DisplayName = displayNameLabel; 
            }
            else if (columnName == HEADER_DISPLAYCOLLECTIONNAME_TH)
            {
                Label displayNameLabel;
                if (entity.DisplayCollectionName == null)
                    displayNameLabel = new Label();
                else
                    displayNameLabel = entity.DisplayCollectionName;

                LocalizedLabel thaiLabel = new LocalizedLabel(
                        input,
                        1054
                    );

                displayNameLabel.LocalizedLabels.Add(thaiLabel);

                entity.DisplayCollectionName = displayNameLabel; 
            }
            else if (columnName == HEADER_DESCRIPTION_TH)
            {
                //-- Do nothing --
            }
            else if (columnName == HEADER_OWNERSHIPTYPE)
            {
                string CAP = input.ToUpper();

                if(CAP == "USER")
                    entity.OwnershipType = OwnershipTypes.UserOwned;
                else
                    entity.OwnershipType = OwnershipTypes.OrganizationOwned;
            }
            else if (columnName == HEADER_ISACTIVITY)
            {
                string CAP = input.ToUpper();

                if( (CAP == "TRUE") || (CAP == "T") || (CAP == "YES") || (CAP == "Y") )
                    entity.IsActivity = true;
                else
                    entity.IsActivity = false;
            }
            else if (columnName == HEADER_P_SCHEMANAME)
            {
                primaryField.SchemaName = input;
            }
            else if (columnName == HEADER_P_REQUIREDLEVEL)
            {
                string CAP = input.ToUpper();

                if(CAP == "NONE")
                {
                    primaryField.RequiredLevel = 
                        new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None);
                }
                else if (CAP == "REQUIRED")
                {
                    primaryField.RequiredLevel = 
                        new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired);
                }
                else if (CAP == "RECOMMENDED")
                {
                    primaryField.RequiredLevel = 
                        new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended);
                }
                else
                {
                    primaryField.RequiredLevel = 
                        new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.SystemRequired);
                }
            }
            else if (columnName == HEADER_P_MAXLENGTH)
            {
                primaryField.MaxLength = Convert.ToInt32(input);
            }
            else if (columnName == HEADER_P_FORMAT)
            {
                primaryField.Format = StringFormat.Text;
            }
            else if (columnName == HEADER_P_DISPLAYNAME_EN)
            {
                Label displayNameLabel;
                if (primaryField.DisplayName == null)
                    displayNameLabel = new Label();
                else
                    displayNameLabel = primaryField.DisplayName;

                LocalizedLabel englishLabel = new LocalizedLabel(
                        input,
                        1033
                    );

                displayNameLabel.LocalizedLabels.Add(englishLabel);

                primaryField.DisplayName = displayNameLabel;
            }
            else if (columnName == HEADER_P_DESCRIPTION_EN)
            {
                primaryField.Description = new Label(input, 1033);
            }
            else if (columnName == HEADER_P_DISPLAYNAME_TH)
            {
                Label displayNameLabel;
                if (primaryField.DisplayName == null)
                    displayNameLabel = new Label();
                else
                    displayNameLabel = primaryField.DisplayName;

                LocalizedLabel thaiLabel = new LocalizedLabel(
                        input,
                        1054
                    );

                displayNameLabel.LocalizedLabels.Add(thaiLabel);

                primaryField.DisplayName = displayNameLabel;
            }
            else if (columnName == HEADER_P_DESCRIPTION_TH)
            {
                //-- Do nothing --
            }
            else if (columnName == HEADER_F_ATTRIBUTEFILE)
            {
                //-- Do nothing --
            }
            else if (columnName == HEADER_F_RELATIONSHIPFILEOTM)
            {
                //-- Do nothing --
            }
            else if (columnName == HEADER_F_RELATIONSHIPFILEMTM)
            {
                //-- Do nothing --
            }
            else 
            {
                throw new Exception(String.Format("ERROR: Unknown HEADER '{0}' - AdaEntityMap.MapField() failed", columnName));
            }
        }//-- End of MapEntityField() --
        #endregion

        #region MapEntityFields()
        //-- method MapEntityFields() --
        //-- Loop through all fields and map the names --
        public static void MapEntityFields(int index, AdaColumns columns, EntityMetadata entity, StringAttributeMetadata primaryField)
        {
            //for (int i = 0; i < columns[index].cellCount; i++)
            //{
                foreach (AdaColumn col in columns)
                {
                    MapEntityField(
                        col.columnName,
                        col.cells[index],
                        entity,
                        primaryField
                        );
                }//-- End of foreach --
            //}//-- End of for --
        }//-- End of MapEntityFields() --
        #endregion
    }//-- End of AdaEntityMap --

    #region class AdaCreateEntitiesFromFiles
    class AdaCreateEntitiesFromFiles
    {
        protected void CreateAnAttribute(
            OrganizationServiceProxy serviceProxy,
            int index,
            string entitySchemaName,
            AdaColumns attributes
            )
        {
            string attributeType;

            attributeType = AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_ATTRIBUTETYPE);

            entitySchemaName = entitySchemaName.ToLower();

            #region if-else series

            #region SingleLineText
            if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_SINGLELINETEXT)
            {
                AdaEntityUtils.CreateASingleLineText(
                    serviceProxy,
                    entitySchemaName,
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_ATTRIBUTESCHEMANAME),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_REQUIREDLEVEL),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_TH),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DESCRIPTION_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_STRINGFORMAT),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MAXLENGTH)
                    );
            }
            #endregion
            #region OptionSet
            else if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_OPTIONSET)
            {
                //-- Do something here --
            }
            #endregion
            #region TwoOptions
            else if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_TWOOPTIONS)
            {
                AdaEntityUtils.CreateABoolean(
                    serviceProxy,
                    entitySchemaName,
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_ATTRIBUTESCHEMANAME),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_REQUIREDLEVEL),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_TH),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DESCRIPTION_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_BOOLEAN01),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_BOOLEAN00)
                    );
            }
            #endregion
            #region WholeNumber
            else if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_WHOLENUMBER)
            {
                AdaEntityUtils.CreateAnInteger(
                    serviceProxy,
                    entitySchemaName,
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_ATTRIBUTESCHEMANAME),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_REQUIREDLEVEL),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_TH),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DESCRIPTION_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MAXIMUMVALUE),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MINIMUMVALUE)
                    );
            }
            #endregion
            #region FloatingPoint
            else if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_FLOATINGPOINT)
            {
                throw new Exception("Floating point attribute is not supported for now");
            }
            #endregion
            #region DecimalNumber
            else if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_DECIMALNUMBER)
            {
                AdaEntityUtils.CreateADecimal(
                    serviceProxy,
                    entitySchemaName,
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_ATTRIBUTESCHEMANAME),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_REQUIREDLEVEL),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_TH),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DESCRIPTION_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MAXIMUMVALUE),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MINIMUMVALUE),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_PRECISION)
                    );
            }
            #endregion
            #region Currency
            else if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_CURRENCY)
            {
                AdaEntityUtils.CreateAMoney(
                    serviceProxy,
                    entitySchemaName,
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_ATTRIBUTESCHEMANAME),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_REQUIREDLEVEL),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_TH),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DESCRIPTION_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MAXIMUMVALUE),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MINIMUMVALUE),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_PRECISION),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_IMEMODE)
                    );
            }
            #endregion
            #region MultipleLineText
            else if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_MULTIPLELINETEXT)
            {
                AdaEntityUtils.CreateAMemo(
                    serviceProxy,
                    entitySchemaName,
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_ATTRIBUTESCHEMANAME),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_REQUIREDLEVEL),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_TH),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DESCRIPTION_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MEMOFORMAT),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_MAXLENGTH),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_IMEMODE)
                    );
            }
            #endregion
            #region DateAndTime
            else if (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_DATEANDTIME)
            {
                AdaEntityUtils.CreateADateTime(
                    serviceProxy,
                    entitySchemaName,
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_ATTRIBUTESCHEMANAME),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_REQUIREDLEVEL),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DISPLAYNAME_TH),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DESCRIPTION_EN),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_DATETIMEFORMAT),
                    AdaCSVUtils.GetStringValue(index, attributes, AdaEntityConstants.HEADER_IMEMODE)
                    );
            }
            #endregion
            #region Others (Throw An Exception)
            else if ((attributeType == "") || (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_UNKNOWN) || (attributeType == AdaEntityUtils.LIST_ATTRIBUTETYPE_LOOKUP))
            {
                throw new Exception("NULL string or unsupported type is returned from GetStringValue() under MapAttribute()");
            }
            #endregion
            #endregion
        }//-- End of CreateAttribute() --

        public void CreateEntityFromCSVFile(OrganizationServiceProxy serviceProxy, bool promptForDelete, string fileName)
        {
            try
            {
                AdaReadCSV adaReadFile = new AdaReadCSV(fileName); 
                AdaColumns columns = new AdaColumns();

                #region Load entities from a file           
                //-- if the file exists, read the entity information from file --
                using (adaReadFile)
                {
                    //-- Read the entity definitation from file --
                    adaReadFile.ReadFile(columns, true);
                }//-- End of Using --

                Console.WriteLine();
                Console.WriteLine("All entities have been loaded from {0} ...", fileName);
                Console.WriteLine("This could be slow, please be patient ...");
                //Console.ReadKey();
                #endregion
                
                #region Create Entities
                using (serviceProxy)
                {
                    // This statement is required to enable early-bound type support.
                    serviceProxy.EnableProxyTypes();

                    //-- Loop through all entities --
                    for (int i = 0; i < columns[0].cellCount; i++)
                    {
                        //-- Create an Entity instance - entity --
                        EntityMetadata entity = new EntityMetadata();

                        //-- Instantiate primaryAttribute --
                        StringAttributeMetadata primaryAttribute = new StringAttributeMetadata();

                        #region Create an entity
                        //-- Map the values from CSV files to entity's data member and create the entity --    
                        try
                        {

                            AdaEntityUtils.CreateEntity(
                                serviceProxy,
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_SCHEMANAME),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_OWNERSHIPTYPE),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_ISACTIVITY),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_DESCRIPTION_EN),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_DISPLAYNAME_EN),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_DISPLAYNAME_TH),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_DISPLAYCOLLECTIONAME_EN),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_DISPLAYCOLLECTIONNAME_TH),

                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_P_SCHEMANAME),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_P_REQUIREDLEVEL),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_P_MAXLENGTH),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_P_FORMAT),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_P_DESCRIPTION_EN),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_P_DISPLAYNAME_EN),
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_P_DISPLAYNAME_TH)
                                );

                            Console.WriteLine("*******************************************************************");
                            Console.WriteLine(String.Format("The new entity '{0}' has been created.",
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_SCHEMANAME)
                                ));
                            Console.WriteLine("*******************************************************************");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("\n");
                            Console.WriteLine("*******************************************************************************");
                            Console.WriteLine("[ERROR]: {0}", e.Message);
                            Console.WriteLine("*******************************************************************************");
                        }
                        #endregion
                        
                        #region Create attributes
                        //-- Create attributes --
                        try
                        {
                            //-- Read the CSV definition from file --
                            AdaReadCSV adaAttributesFile = new AdaReadCSV(
                                AdaCSVUtils.GetStringValue(i, columns, AdaEntityMap.HEADER_F_ATTRIBUTEFILE));

                            AdaColumns attributes = new AdaColumns();

                            //-- Scope the objects under using() --
                            using (adaAttributesFile)
                            {
                                //-- Load attribute file --
                                adaAttributesFile.ReadFile(attributes, true);

                                //-- Loop through all attributes --
                                for (int j = 0; j < attributes[0].cellCount; j++)
                                {
                                    //-- Display attribute schema name --
                                    Console.WriteLine(AdaCSVUtils.GetStringValue(j, attributes, AdaEntityConstants.HEADER_SCHEMANAME));

                                    //-- Create an attribute --
                                    try
                                    {
                                        CreateAnAttribute(
                                            serviceProxy, 
                                            j,
                                            AdaCSVUtils.GetStringValue(i, columns, AdaEntityConstants.HEADER_SCHEMANAME), 
                                            attributes);
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine();
                                        Console.WriteLine(
                                            String.Format(
                                            "!! ERROR !! Message: \"{0}\"",
                                            e.Message
                                            ));
                                    }
                                }//-- End of for() --
                            }//-- End of using(adaAttributesFile) --
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine();
                            Console.WriteLine(
                                String.Format(
                                "!! ERROR !! Message: \"{0}\"",
                                e.Message
                                ));
                        }//-- End of catch --
                        #endregion
                                             
                    }//-- End of for "Entities" --

                    Console.WriteLine("\nCreate entities completed!!");
                    Console.WriteLine("###################################################################");
                    Console.WriteLine();
                    Console.WriteLine();
                }//-- End of using(serviceProxy...) -- 
                #endregion

                #region  Entities

                bool deleteEntity = true;

                Console.Write("\nDo you want these custom entities deleted? (y/n): ");
                String answer = Console.ReadLine();

                deleteEntity = (answer.StartsWith("y") || answer.StartsWith("Y"));

                if (!deleteEntity)
                {
                    Console.WriteLine("\n!! BYE !!");
                    Console.WriteLine("###################################################################");
                    return;
                }

                for (int i = 0; i < columns[0].cellCount; i++)
                {
                    EntityMetadata entity = new EntityMetadata();
                    StringAttributeMetadata primaryAttribute = new StringAttributeMetadata();
                    string entitySchemaName = AdaCSVUtils.GetStringValue(i, columns, AdaEntityMap.HEADER_SCHEMANAME).ToLower();

                    AdaEntityUtils.DeleteEntity(serviceProxy, entitySchemaName);

                    Console.WriteLine(
                        String.Format("{0} is deleted",
                        entitySchemaName)
                        );
                }//-- End of For --

                Console.WriteLine("\nDelete entities completed!!");
                Console.WriteLine("###################################################################");
                #endregion
            }
            catch (Exception e)
            {
                //-- Do something here --
                throw e;
            }
        }//-- End of CreateEntityFromCSVFile() --
    }//-- End of class AdaCreateEntitiesFromFiels --
    #endregion
}
